#include "intArray_t.h"

intArray_t::intArray_t() {
	_capacity = init_capacity; 
	_numOfElements = 0; 
	_expandValue = init_capacity;
	_array =  new int*[_capacity]; 

}

intArray_t::intArray_t(const int& iniateElementsNum){
	_numOfElements = iniateElementsNum;
	_expandValue = init_capacity;
	_capacity = iniateElementsNum + init_capacity;
	 _array = new int*[_capacity];
}


intArray_t::intArray_t(const intArray_t &t) {
}


intArray_t::~intArray_t() {
	delete[] _array;
}




void intArray_t::copyArrays(int** newArray){
	for(int i = 0; i < _capacity ; i++)
			newArray[i] = _array[i];
		delete[] _array;
		_array = newArray; 
}
void intArray_t::insertNewElement(int* const &element){
	if (_numOfElements == _capacity){
		int** arr = new int*[_capacity+_expandValue];
		copyArrays(	arr);
		_capacity= _capacity + _expandValue;
	}
	_array[_numOfElements++] = element;
}


int intArray_t::getNumberOfElements() const
{
	return _numOfElements;
}

int intArray_t::getCapacity() const {
	return _capacity;
}

int* intArray_t::getFirstElement() const{
	if (_numOfElements == 0) return 0;
		else return _array[0];
}

int* intArray_t::getLastElement() const{
	if (_numOfElements == 0) return 0;
		else return _array[_numOfElements - 1];
}

int* intArray_t::find(const int& value) const{
	for (int i =0;i < _numOfElements ; i++)
		if (*_array[i] == value)
			return _array[i];
	return 0;
}

int* intArray_t::remove(const int& value) {
	for (int i = 0; i < _numOfElements; i++){
		if ( *_array[i] == value ){
			int* p = _array[i];
			for (int j = i; j < _numOfElements -1; j++) 
			_array[j] = _array[j+1];
			_numOfElements--;
			return p;
		}
	 }
	return 0;
}

void intArray_t::removeAll(){
	_numOfElements = 0;
}

void intArray_t::removeAndDeleteElement(const int& num){
	int* p = remove(num); 
	if (p != 0 ) {
		delete p;
	}
	return;
}

void intArray_t::removeAndDeleteAll(){
	for (int i = 0;i < _numOfElements ; i++){
		delete _array[i];
		}
		_numOfElements = 0;
	}

	
void intArray_t::append(const int& index, int* const &element){
	if (index < 0 ||index >= _numOfElements)
		return ;
	if ( ++_numOfElements  > _capacity){
		int** arr = new int*[_capacity+_expandValue];
		copyArrays(arr);
		_capacity= _capacity+_expandValue;
	}
	for (int i = _numOfElements -2; i > index ; i--)
		_array[i+1] = _array[i];
	_array[index+1] = element;
}

void intArray_t:: prepend(const int& index, int* const &element){
	if (index < 0 || index >= _numOfElements)
		return ;
	if ( ++ _numOfElements  > _capacity){
		int** arr = new int*[_capacity+_expandValue];
		copyArrays(arr);
		_capacity= _capacity+_expandValue;
	}
	for (int i = _numOfElements -2; i > index -1 ; i--)
		_array[i+1] = _array[i];
	_array[index] = element;

}

bool intArray_t::isEmpty() {
	return (_numOfElements == 0);
}

